package com.mooc;

import android.os.Bundle;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.xiaomi.mipush.sdk.MiPushClient;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by Administrator on 2017/7/4 0004.
 */


public class MiPushModule extends ReactContextBaseJavaModule {

    private static final String APP_ID = "2882303761517582688";
    private static final String APP_KEY = "5551758224688";
    //private static String deviceId = null;
    private static Boolean registered = false;
    private static MiPushModule gModules = null;
    ReactApplicationContext mContext;
    public MiPushModule(ReactApplicationContext reactContext){
        super(reactContext);
        mContext = reactContext;
    }

    @Override
    public String getName() {
        return "MiPushModule";
    }


    @ReactMethod
    public void setUserAccount(String userAccount) {
        MiPushClient.setUserAccount(mContext, userAccount, null);
    }

    @ReactMethod
    public void unsetUserAccount(String userAccount) {
        MiPushClient.unsetUserAccount(mContext, userAccount, null);
    }

    @ReactMethod
    public void registerPush() {
        MiPushClient.registerPush(mContext, APP_ID, APP_KEY);
        registered = true;
        gModules = this;
    }
    public static void sendEvent(String key,final Bundle bundle) {
       switch(key){
           case "onNotificationMessageArrived":
           case "onReceivePassThroughMessage":
               //Log.i("System.out", String.valueOf(gModules)+"111Arrived");
               WritableMap message = Arguments.fromBundle(bundle);
               DeviceEventManagerModule.RCTDeviceEventEmitter emitter =
                       gModules.getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
               emitter.emit("mipush", message);
               //Log.i("System.out", String.valueOf(gModules)+"2222Arrived");
               return;

           case "onNotificationMessageClicked":
               TimerTask task = new TimerTask(){
                   public void run(){
                       //Log.i("System.out", String.valueOf(gModules)+"333Clicked");
                       WritableMap message = Arguments.fromBundle(bundle);
                       DeviceEventManagerModule.RCTDeviceEventEmitter emitter =
                               gModules.getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
                       emitter.emit("mipush", message);
                       //Log.i("System.out", String.valueOf(gModules)+"444Clicked");
                   }
               };
               Timer timer = new Timer();
               timer.schedule(task, 3000);
               return;

       }

    }

}
